package com.tourism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourism.common.Result;
import com.tourism.entity.LodgingInfo;
import com.tourism.mapper.LodgingInfoMapper;
import com.tourism.service.LodgingInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class LodgingInfoServiceImpl extends ServiceImpl<LodgingInfoMapper, LodgingInfo> implements LodgingInfoService {

    @Override
    public Result getLodgingById(Integer id) {
        if (id == null) {
            return Result.fail("ID不能为空");
        }
        LodgingInfo lodgingInfo = getById(id);
        return Result.success(lodgingInfo);
    }

    @Override
    public Result getLodgingList(Integer page, Integer pageSize, String name) {
        Page<LodgingInfo> pageParam = new Page<>(page, pageSize);
        LambdaQueryWrapper<LodgingInfo> wrapper = new LambdaQueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            wrapper.like(LodgingInfo::getName, name);
        }

        Page<LodgingInfo> pageResult = page(pageParam, wrapper);
        return Result.success(pageResult);
    }

    @Override
    public Result addLodging(LodgingInfo lodgingInfo) {
        if (lodgingInfo.getName() == null || lodgingInfo.getName().isEmpty()) {
            return Result.fail("民宿名称不能为空");
        }
        boolean success = save(lodgingInfo);
        return success ? Result.success("添加成功") : Result.fail("添加失败");
    }

    @Override
    public Result updateLodging(LodgingInfo lodgingInfo) {
        if (lodgingInfo.getId() == null) {
            return Result.fail("ID不能为空");
        }
        boolean success = updateById(lodgingInfo);
        return success ? Result.success("更新成功") : Result.fail("更新失败");
    }

    @Override
    public Result deleteLodging(Integer id) {
        if (id == null) {
            return Result.fail("ID不能为空");
        }
        boolean success = removeById(id);
        return success ? Result.success("删除成功") : Result.fail("删除失败");
    }

    @Override
    @Transactional
    public Result batchDeleteLodgings(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.fail("ID列表不能为空");
        }
        boolean success = removeBatchByIds(ids);
        return success ? Result.success("批量删除成功") : Result.fail("批量删除失败");
    }

    @Override
    public Result getAllLodgings() {
        List<LodgingInfo> lodgings = list();
        return Result.success(lodgings);
    }
}
